package sy4;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class test04 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/testDB?useSSL=false&serverTimezone=UTC";
        String user = "root";
        String password = "123456";

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            String sql = "INSERT INTO Student (PlayerID, PlayerName, PlayerClass, TeacherID) VALUES (?, ?, ?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            // 批量添加数据
            pstmt.setInt(1, 5);
            pstmt.setString(2, "王五");
            pstmt.setString(3, "三班");
            pstmt.setInt(4, 103);
            pstmt.addBatch();

            pstmt.setInt(1, 6);
            pstmt.setString(2, "赵六");
            pstmt.setString(3, "四班");
            pstmt.setInt(4, 104);
            pstmt.addBatch();

            // 执行批量操作
            int[] result = pstmt.executeBatch();
            System.out.println("批量插入影响行数：" + result.length);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}